<?php
session_start();
if(isset($_COOKIE['loginck'])) {
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Tạo album ảnh</title>
<script type="text/javascript" src="js/jquery-1.3.2.js"></script>
<script type="text/javascript" src="js/swfupload/swfupload.js"></script>
<script type="text/javascript" src="js/jquery.swfupload.js"></script>
<script type="text/javascript" src="js/jquery-validation-1.9.0/jquery.validate.min.js"></script>  

<script type="text/javascript" src="js/validate.js"></script>
<script type="text/javascript">

$(function(){
	$('#swfupload-control').swfupload({
		upload_url: "upload-file.php",
		file_post_name: 'uploadfile',
		file_size_limit : "1024",
		file_types : "*.jpg;*.png;*.gif",
		file_types_description : "Image files",
		file_upload_limit : 100,
		flash_url : "js/swfupload/swfupload.swf",
		button_image_url : 'js/swfupload/wdp_buttons_upload_114x29.png',
		button_width : 114,
		button_height : 29,
		button_placeholder : $('#button')[0],
		debug: false
	})
		.bind('fileQueued', function(event, file){
			var listitem='<li id="'+file.id+'" >'+
				'File: <em>'+file.name+'</em> ('+Math.round(file.size/1024)+' KB) <span class="progressvalue" ></span>'+
				'<div class="progressbar" ><div class="progress" ></div></div>'+
				'<p class="status" >Pending</p>'+
				'<span class="cancel" >&nbsp;</span>'+
				'</li>';
			$('#log').append(listitem);
			$('li#'+file.id+' .cancel').bind('click', function(){
				var swfu = $.swfupload.getInstance('#swfupload-control');
				swfu.cancelUpload(file.id);
				$('li#'+file.id).slideUp('fast');
			});
			// start the upload since it's queued
			$(this).swfupload('startUpload');
		})
		.bind('fileQueueError', function(event, file, errorCode, message){
			alert('Size of the file '+file.name+' is greater than limit');
		})
		.bind('fileDialogComplete', function(event, numFilesSelected, numFilesQueued){
			$('#queuestatus').text('Files Selected: '+numFilesSelected+' / Queued Files: '+numFilesQueued);
			
		})
		.bind('uploadStart', function(event, file){
			$('#log li#'+file.id).find('p.status').text('Uploading...');
			$('#log li#'+file.id).find('span.progressvalue').text('0%');
			$('#log li#'+file.id).find('span.cancel').hide();
		})
		.bind('uploadProgress', function(event, file, bytesLoaded){
			//Show Progress
			var percentage=Math.round((bytesLoaded/file.size)*100);
			$('#log li#'+file.id).find('div.progress').css('width', percentage+'%');
			$('#log li#'+file.id).find('span.progressvalue').text(percentage+'%');
		}) 
		
		.bind('uploadSuccess', function(event, file, serverData){
			var item=$('#log li#'+file.id);
			item.find('div.progress').css('width', '100%');
			item.find('span.progressvalue').text('100%');
			//var pathtofile='<a href="uploads/'+file.name+'" target="_blank" >view &raquo;</a>';
			item.addClass('success').find('p.status').html('Done!!!');// | '+pathtofile);
		}) 
		.bind('uploadComplete', function(event, file){
			// upload has completed, try the next one in the queue
			$(this).swfupload('startUpload');
		})
	
});	

</script>
<style type="text/css" >
#abc {
	margin-left: 300px;
	margin-top: 50px;
	
}
#swfupload-control p{ margin:10px 5px; font-size:0.9em; }
#log{ margin:0; padding:0; width:500px;}
#log li{ list-style-position:inside; margin:2px; border:1px solid #ccc; padding:10px; font-size:12px; 
	font-family:Arial, Helvetica, sans-serif; color:#333; background:#fff; position:relative;}
#log li .progressbar{ border:1px solid #333; height:5px; background:#fff; }
#log li .progress{ background:#999; width:0%; height:5px; }
#log li p{ margin:0; line-height:18px; }
#log li.success{ border:1px solid #339933; background:#ccf9b9; }
#log li span.cancel{ position:absolute; top:5px; right:5px; width:20px; height:20px; 
	background:url('js/swfupload/cancel.png') no-repeat; cursor:pointer; }
#albm input[type=submit] {
	color:#FFF;
	background:#333;
	padding:10px;
	border:2px solid #ddd;
	font-size: 1.2em;
	box-shadow: 0px 0px 20px #999;
	-moz-box-shadow:0px 0px 20px #999; /*firefox*/
	-webkit-box-shadow:0px 0px 20px #999; /*chrome*/
	
	position:relative;
	top: 30px;
}
#mtal {
 position:absolute;
 top: 50px;
 left:100px;	
}
</style>
</head>
<body>
<div id="abc">
	<h3>&raquo; Tạo album ảnh mới</h3>
<div id="mtal">
            <embed src="flash/flashvortex.swf" quality="best" menu="true" width="51" height="63" type="application/x-shockwave-flash" />	
</div>
<div id="swfupload-control">
	<p>Upload tối đa 100 files (jpg, png, gif),kích thước dưới 1MB</p>
	<input type="button" id="button" />
	<p id="queuestatus" ></p>
	<ol id="log"></ol>
</div>
<div id="albm">
<form method="post" action="" id="alname_form" > <!--validate ko hoat dong?-->
Tên album :
<input type="text" name="tenal" value="Create Album" class="required"> <br/> 
<input type="submit" value="Tạo album" name="okeal">
<input type="submit" value="Hủy" name="huyupdate"> <!--xóa ảnh trong file update?-->
</form>
</div>
</div>
<?php
// chen album, anh vao trong csdl, them nguoi dang, ngay dang, => thong tin day du de chen vao csdl cho bang anh, album
if(isset($_POST['okeal'])) {
	include "cnn.php";
	$alb_name = $_POST['tenal'];// ten album
	$use_post = $_COOKIE['loginck'];
	$sqlu_p = "select use_id from lib_user where use_username='".$use_post."'"; 
	$queryu_p = mysql_query($sqlu_p) or die("lay ra id nguoi dang:".mysql_error());
	$use_id = mysql_fetch_array($queryu_p); // ma nguoi dang
	$now = getdate();
	$ngaydang = $now['mday'];
	$thangdang = $now['mon'];
	$namdang = $now['year'];
	$array_datepost = array($namdang,$thangdang,$ngaydang);
	$alb_datepost = implode('-',$array_datepost); // ngay thang nam dang bai
	$alb_question = "?";
	///////////////////////////////////////////
	// chen thong tin album
	$sqlal = "INSERT INTO lib_album(alb_name,use_id,alb_datepost,alb_question)values('".$alb_name."','".$use_id['use_id']."','".$alb_datepost."','".$alb_question."')";
	$queryal = mysql_query($sqlal) or die("csdl cho album: ".mysql_error());
	//lay ra alb_id
	$sqlid = "select alb_id from lib_album";
	$queryid = mysql_query($sqlid) or die("loi lay id album: ".mysql_error());
	$numid = mysql_num_rows($queryid);
	
	$sqlida = "select alb_id from lib_album order by alb_id DESC limit ".$numid." ";
	$queryida = mysql_query($sqlida) or die("loi lay id album2: ".mysql_error());
	$rowida = mysql_fetch_array($queryida);
	$alb_id = $rowida[0];
	$_SESSION['alb_id'] = $rowida[0];
	//////////////////////////////////////////
	// chen thong tin anh trong album
	///////////lay ra tat ca anh trong file album vua tao chen vao csdl
	
	
	
	$path = "./uploads/".$alb_id."/"; 
           $files = array();
		   if(is_dir($path)) {
             $dir = opendir($path);
           while ($f = readdir($dir)) {
                //if (eregi("\.jpg|\.gif|\.png", $f))
                  array_push($files, $f);
           }
           closedir($dir);
           $data = array();
           if (! empty($files)) {
                foreach ($files as $k => $v) {
                          //$v la ten anh
						  if(($v != ".")&($v != "..")) {
	                      $sqlphoto = "INSERT INTO lib_image(img_name,img_datepost,alb_id)values('".$v."','".$alb_datepost."','".$alb_id."')";
	                       $queryphoto = mysql_query($sqlphoto) or die("loi anh trong album: ".mysql_error());
						  }
                 }
           }
	
		   }
	
	echo "<script>alert('da them album');</script>";

if(isset($_POST['huyupdate'])) {
	// xoa anh trong file upload tren may chu => o day la xoa thu muc
	$dir ="./uploads/".($_SESSION['alb_id']+1)."/";
	
	if (is_dir($dir)) {
        $objects = scandir($dir);
        foreach ($objects as $object) {
            if ($object != "." && $object != "..") {
                if (filetype($dir."/".$object) == "dir") remove_dir($dir."/".$object);
                else unlink($dir."/".$object);
            }
        }
        reset($objects);
        rmdir($dir);
    }
}
}
?>
</body>
</html>
<?php
}
else {
	
	$myun = "xin loi, ban chua dang nhap<br/>";
	echo $myun ;
	echo "<embed src='flash/flashvortex.swf' width='50' height='50'/>";
	
}
?>